package com.ymatou.iapi.shoppingcartservice.testcase;

import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductBean;
import com.ymatou.iapi.shoppingcartservice.service.AddShoppingCartCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.mongo.YmtShoppingCartWapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.compare.AssertHelper;

public class Ts_AddShoppingCart {
	
	private static AddShoppingCartCall addShoppingCartCall = new AddShoppingCartCall();
	private static int userId = 32145;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("添加商品");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(addShoppingCartCall.getOpurl());
	}	
	
	@After
	public void caseDown() {
		Logger.end(); 
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AddShoppingCart_001() throws Exception {
		Logger.start(true,"购物车添加单个商品");
		try{
			YmtShoppingCartWapper ymtShoppingCartWapper = new YmtShoppingCartWapper();
			ShoppingcartProductBean shoppingcartProductBean = AddShoppingCartCall.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
					MapUtil.hashMap("userId", userId));
			System.out.print(shoppingcartProductBean.getCreateTime());
			JSONObject ret = addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
			int code = ret.getInt("code");
			
			Assert.assertEquals(200, code);
			Map<String,Object> shoppingcartProductDB = ymtShoppingCartWapper.
					getByProductIdAndUserId(shoppingcartProductBean.getProductId(), userId);
			
			ShoppingcartProductBean actBean = new ShoppingcartProductBean();
			actBean.setShoppingCartId(shoppingcartProductDB.get("_id").toString());
			actBean.setCreateTime(shoppingcartProductDB.get("createtime").toString());
			actBean.setLastModifiedTime(shoppingcartProductDB.get("lastmodifiedtime").toString());
			actBean.setEndTime(shoppingcartProductDB.get("endtime").toString());
			actBean.setUserId(Integer.parseInt(shoppingcartProductDB.get("userid").toString()));
			actBean.setSellerId(Integer.parseInt(shoppingcartProductDB.get("sellerid").toString()));
			actBean.setCatalogId(shoppingcartProductDB.get("catalogid").toString());
			actBean.setProductId(shoppingcartProductDB.get("productid").toString());
			actBean.setNum(Integer.parseInt(shoppingcartProductDB.get("num").toString()));
			actBean.setStatus(Integer.parseInt(shoppingcartProductDB.get("status").toString()));
			actBean.setSource(Integer.parseInt(shoppingcartProductDB.get("source").toString()));
			actBean.setClient(Integer.parseInt(shoppingcartProductDB.get("client").toString()));
			actBean.setMainOrderId(shoppingcartProductDB.get("mainorderid").toString());
			actBean.setProductName(shoppingcartProductDB.get("productname").toString());
			actBean.setDeliveryType(Integer.parseInt(shoppingcartProductDB.get("deliverytype").toString()));
			actBean.setTariffType(Integer.parseInt(shoppingcartProductDB.get("tarifftype").toString()));
			actBean.setSkuInfo(shoppingcartProductDB.get("skuinfo").toString());
			actBean.setPrice(Double.parseDouble(shoppingcartProductDB.get("price").toString()));
			actBean.setPic(shoppingcartProductDB.get("pic").toString());
			actBean.setProductType(Integer.parseInt(shoppingcartProductDB.get("producttype").toString()));
			actBean.setRefundType(Integer.parseInt(shoppingcartProductDB.get("refundtype").toString()));
			actBean.setValidEnd(shoppingcartProductDB.get("validend").toString());
			actBean.setValidStart(shoppingcartProductDB.get("validstart").toString());
			actBean.setSellerName(shoppingcartProductDB.get("sellername").toString());
			actBean.setSellerLogo(shoppingcartProductDB.get("sellerlogo").toString());
			actBean.setSellerCountry(shoppingcartProductDB.get("sellercountry").toString());
			actBean.setSellerFlag(shoppingcartProductDB.get("sellerflag").toString());
			actBean.setActivityId(Integer.parseInt(shoppingcartProductDB.get("activityid").toString()));
			actBean.setNoReasonReturn(Boolean.getBoolean(shoppingcartProductDB.get("isnoreasonreturn").toString()));
			actBean.setActivityName(shoppingcartProductDB.get("activityname").toString());
			actBean.setSingleCatalog(Boolean.getBoolean(shoppingcartProductDB.get("issinglecatalog").toString()));
			actBean.setVersion(shoppingcartProductDB.get("version").toString());
			actBean.setAddPrice(Double.parseDouble(shoppingcartProductDB.get("addprice").toString()));
			actBean.setStock(Integer.parseInt(shoppingcartProductDB.get("stock").toString()));
			
			AssertHelper.assertResultEqual(shoppingcartProductBean, actBean, "");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
